package org.eccl.platform.rde.web.controller;



import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eccl.platform.rde.web.component.RdeClient;
import org.eccl.platform.rde.web.component.entity.ConceptInfo;
import org.eccl.platform.rde.web.component.entity.ScopeInfo;
import org.eccl.platform.rde.web.controller.entity.ConceptInfoForPage;
import org.eccl.platform.rde.web.controller.entity.PathItem;
import org.eccl.platform.rde.web.controller.entity.ScopeInfoForPage;
import org.jboss.resteasy.client.spring.RestClientProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RdeController {
	
	@RequestMapping("/scopeInfo")
	public String getScopeInfo(@RequestParam("scopePath")List<String> scopePath, Model model) throws Exception {
		ScopeInfo scopeInfo = rdeClient.getObject().getScopeInfo(scopePath);
		model.addAttribute("scope", getScopeInfoForPage(scopeInfo));
		return "scope";
	}
	
	
	private ScopeInfoForPage getScopeInfoForPage(ScopeInfo scopeInfo) {
		ScopeInfoForPage scopeInfoForPage = new ScopeInfoForPage();
		List<String> scopePath  = scopeInfo.getPath();
		List<PathItem> path = new ArrayList<>();
		List<String> subPath = new ArrayList<>();
		for (String text : scopePath) {
			PathItem pathItem = new PathItem();
			pathItem.setText(text);
			subPath = new ArrayList<>(subPath);
			subPath.add(text);
			pathItem.setPath(subPath);
			path.add(pathItem);
		}
		scopeInfoForPage.setPath(path);
		
		Set<ConceptInfo> conceptSet = scopeInfo.getConceptSet();
		List<ConceptInfoForPage> conceptList = new ArrayList<>();
		for (ConceptInfo conceptInfo : conceptSet) {
			ConceptInfoForPage conceptInfoForPage = new ConceptInfoForPage();
			List<String> conceptPath = conceptInfo.getPath();
			conceptInfoForPage.setName(conceptPath.get(conceptPath.size() - 1));
			conceptInfoForPage.setPath(conceptPath);
			conceptInfoForPage.setRelateToScope(conceptInfo.isRelateToScope());
			conceptList.add(conceptInfoForPage);
		}
		scopeInfoForPage.setConceptList(conceptList);
		
		return scopeInfoForPage;
	}
	
	
	@Autowired
	private RestClientProxyFactoryBean<RdeClient> rdeClient;
}
